package de.gwtprime.client.views;


import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.Widget;
import com.google.web.bindery.requestfactory.shared.Receiver;

import de.gwtprime.client.ClientFactory;
import de.gwtprime.client.event.OrderProcessedEvent;
import de.gwtprime.client.event.PaymentTriggeredEvent;
import de.gwtprime.client.event.handler.OrderProcessedEventHandler;
import de.gwtprime.client.event.handler.PaymentTriggeredEventHandler;
import de.gwtprime.client.widgets.ArticlePreviewWidgetImpl;
import de.gwtprime.client.widgets.trading.TradingOverview;
import de.gwtprime.client.widgets.trading.TradingTable;
import de.gwtprime.client.widgets.trading.TradingTable.BuySellTable;
import de.gwtprime.shared.requestFactory.ArticleProxy;
import de.gwtprime.shared.requestFactory.OrderProxy;

public class ArticleOverviewViewImpl extends Composite implements ArticleOverviewView {

	private static ArticleOverviewViewImplUiBinder uiBinder = GWT.create(ArticleOverviewViewImplUiBinder.class);
	
	interface ArticleOverviewViewImplUiBinder extends UiBinder<Widget, ArticleOverviewViewImpl>{}
	
	@UiField FlowPanel articlePanel;
	
	private Presenter presenter;
	
	private String name;

	ClientFactory clientFactory;
	
	TradingTable ownOrdersTable;
	
	public ArticleOverviewViewImpl(final ClientFactory clientFactory){
		
		initWidget(uiBinder.createAndBindUi(this));
		this.clientFactory = clientFactory;
		
		
		articlePanel.add(new TradingOverview(clientFactory));
		
		articlePanel.addStyleName("center");
		
		clientFactory.getRequestFactory().orderRequest().loadOrders(clientFactory.getUser()).with("seller", "buyer").fire(new Receiver<List<OrderProxy>>() {

			@Override
			public void onSuccess(List<OrderProxy> response) {
				
				ownOrdersTable = new TradingTable(clientFactory, response, BuySellTable.OWN_ORDERS);
				articlePanel.add(ownOrdersTable);
			}
		});
		
		clientFactory.getEventBus().addHandler(OrderProcessedEvent.TYPE, new OrderProcessedEventHandler() {
			
			@Override
			public void onOrderProcessed(OrderProcessedEvent event) {
				updateOwnOrders(clientFactory);
				
			}

		});
		
		clientFactory.getEventBus().addHandler(PaymentTriggeredEvent.TYPE, new PaymentTriggeredEventHandler() {
			
			@Override
			public void onPaymentTriggered(PaymentTriggeredEvent event) {
				updateOwnOrders(clientFactory);
			}
		});
		
	}
	private void updateOwnOrders(final ClientFactory clientFactory) {
		clientFactory.getRequestFactory().orderRequest().loadOrders(clientFactory.getUser()).with("seller", "buyer").fire(new Receiver<List<OrderProxy>>() {
			
			@Override
			public void onSuccess(List<OrderProxy> response) {
				ownOrdersTable.updateOrders(response);
			}
		});
	}
	
	@Override
	public void setName(String name) {
		this.name = name;
	}
	
	public String getName(){
		return name;
	}

	@Override
	public void setPresenter(Presenter presenter) {
		this.presenter = presenter;

	}
	
	public Presenter getPresenter(){
		return presenter;
	}
	
	@Override
	public void setArticles(List<ArticleProxy> articles) {
		
		articlePanel.clear();
		
		for(ArticleProxy article : articles){
			ArticlePreviewWidgetImpl articleWidget = new ArticlePreviewWidgetImpl(clientFactory, article);
			articlePanel.add(articleWidget);
			
		}
		
		
	}

	
	
	

}
